﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_TouShu_Manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateDrop();
        }
        txtGetUserName.Attributes.Add("ReadOnly", "true");
        txtADdUserID.Attributes.Add("ReadOnly", "true");
    }


    public void PopulateDrop()
    {
        drpDXLX.PopulateFromEnum<TouShu.type>();
        drpDXLX.Items.Insert(0, new ListItem("请选择", "0"));
        drpStatus.PopulateFromEnum<TouShu.status>();
        drpStatus.Items.Insert(0, new ListItem("请选择", "0"));
    }
    protected void Pager_PreRender(object sender, EventArgs e)
    {
        PopulateTouShu();
    }

    public void PopulateTouShu()
    {
        lvDate.DataSource = TouShu.GetAllByConditon(DongDong.Common.Misc.ConvertToInt(drpDXLX.SelectedValue), DongDong.Common.Misc.ConvertToInt(hdlGetUserID.Value), DongDong.Common.Misc.ConvertToInt(drpStatus.SelectedValue), txtAddTimeFrom.Text, txtAddTimeTo.Text, DongDong.Common.Misc.ConvertToInt(hdlAddUserID.Value), txtKey.Text);
        lvDate.DataBind();
    }
    protected void lvDate_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvDate.EditIndex = -1;
        PopulateTouShu();
    }
    protected void lvDate_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int KeyID = DongDong.Common.Misc.ConvertToInt(lvDate.DataKeys[e.ItemIndex].Value);
        TouShu.BadDelete(KeyID);
        lvDate.EditIndex = -1;
        PopulateTouShu();
    }

    protected void lvDate_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int KeyID = DongDong.Common.Misc.ConvertToInt(lvDate.DataKeys[e.ItemIndex].Value);
        var item = lvDate.Items[e.ItemIndex];

        DropDownList drpStatus = (DropDownList)item.FindControl("drpStatus");
        TextBox txtResult = (TextBox)item.FindControl("txtResult");
        if (string.IsNullOrEmpty(txtResult.Text))
        {
            DongDong.Common.MessageBox.Show(this, "处理结果不能为空");
            return;
        }
        TouShu objTouShu = TouShu.GetByID(KeyID);
        objTouShu.Status = DongDong.Common.Misc.ConvertToInt(drpStatus.SelectedValue);
        objTouShu.Result = txtResult.Text;
        objTouShu.Update();

        lvDate.EditIndex = -1;
        PopulateTouShu();
    }

    public string GetUserName(object UserID)
    {
        string Return = "暂无资料";
        Client objClient = Client.Get(DongDong.Common.Misc.ConvertToInt(UserID));
        if (objClient != null)
        {
            Return = "<a target='_blank' href='../../Admin/Client/Add.aspx?UserID='" + objClient.ID + ">" + objClient.Username + "</a>";
        }
        return Return;
    }

    protected void lvDate_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvDate.EditIndex = e.NewEditIndex;
        PopulateTouShu();

        var item = lvDate.Items[lvDate.EditIndex];
        DropDownList drpStatus = (DropDownList)item.FindControl("drpStatus");
        drpStatus.PopulateFromEnum<TouShu.status>();

        int KeyID = DongDong.Common.Misc.ConvertToInt(lvDate.DataKeys[e.NewEditIndex].Value);
        TouShu objTouShu = TouShu.GetByID(KeyID);

        drpStatus.SelectedValue = objTouShu.Status.ToString();
    }
    protected void lvDate_PagePropertiesChanged(object sender, EventArgs e)
    {
        PopulateTouShu();
    }

}